package chess;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class Grid {

	private int width;
	private int height;
	private HashMap<Coordinate, Piece> map;

	public Grid(){
		this.width= GameRunner.X;
		this.height= GameRunner.Y;
		this.map= new HashMap<Coordinate, Piece>(64);
		for (int i=0; i<GameRunner.X; i++){
			for (int j=0; j<GameRunner.Y; j++){
				map.put(new Coordinate(i,j), null);
			}
		}
	}

	public Piece getPiece(Coordinate c){
		Set<Coordinate> keys= map.keySet();
		for (Coordinate coord: keys){
			if (c.equals(coord)){
				return map.get(coord);
			}
		}
		return null;
	}
	
	public Piece getPiece(int x, int y) {
		for (Coordinate coord:map.keySet()) {
			if (x == coord.getX() && y == coord.getY()) {
				return map.get(coord);
			}
		}
		return null;
	}
	
	public void setPieceToCoordinate(Coordinate c, Piece p){
		for (Coordinate coord: map.keySet()){
			if (c.equals(coord)){
				map.put(coord, p);
			}
		}
	}

	public boolean isValid(Coordinate c){
		return (c.getX()< GameRunner.X && c.getX()>= 0 && c.getY()< GameRunner.Y && c.getY()>= 0);
	}

	public boolean isOccupied(Coordinate c){
		if (map.get(c)!= null && isValid(c))
			return true;
		else 
			return false;
	}
	
	public String toString(){
		String s= new String("");
		Set<String> strings = new HashSet<String>();
		for (Coordinate c: map.keySet()){
			if (map.get(c)!= null) s= c.toString()+ " - " + map.get(c).toString() + "\n";
			else s= c.toString()+ " - " + "null" + "\n";
			strings.add(s);
		}
		return strings.toString();
	}

}